package com.gmscosmo.janus.controller;

import com.alibaba.fastjson.JSONObject;
import com.gmscosmo.common.APIPath;
import com.gmscosmo.common.IMResult;
import com.gmscosmo.janus.engine.AVEngineKit;
import com.gmscosmo.request.ConfigureBody;
import com.gmscosmo.request.CreateRoomBody;
import com.gmscosmo.request.JoinBody;
import com.gmscosmo.request.Jsep;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author stephen.qiu
 */
@RestController
public class EngineController {
    @Autowired
    AVEngineKit avEngineKit;

    /**
      {
       "createRoomBody":{
           "room":999991,
           "is_private":true,
           "pin":"123456",
           "publishers":9
       },
       "userId":"stephen"
      }
     */
    @PostMapping(APIPath.CREATE_ROOM)
    public IMResult<String> handleCreateRoom(@RequestBody JSONObject jsonObject) {
        IMResult<String> result = new IMResult<>();
        String userId = jsonObject.getString("userId");
        CreateRoomBody createRoomBody = jsonObject.getObject("createRoomBody", CreateRoomBody.class);
        String response = null;
        try {
            response = avEngineKit.handleCreateRoom(userId, createRoomBody);
        } catch (Exception e) {
            e.printStackTrace();
        }
        result.setResult(response);
        return result;
    }

    /**
      {
       "joinBody":{
           "ptype":"publisher",
           "pin":"123456",
           "room":999991,
           "display":"stephen"
       },
       "userId":"stephen"
      }
     */
    @PostMapping(APIPath.JOIN_PUB)
    public IMResult<String> handleJoinPub(@RequestBody JSONObject jsonObject) {
        IMResult<String> result = new IMResult<>();
        String userId = jsonObject.getString("userId");
        JoinBody joinBody = jsonObject.getObject("joinBody", JoinBody.class);
        String response = null;
        try {
            response = avEngineKit.handleJoinAndPub(userId, joinBody);
        } catch (Exception e) {
            e.printStackTrace();
        }
        result.setResult(response);
        return result;
    }

    /**
      {
       "configureBody":{
           "audio":true,
           "pin":"123456",
           "request":"configure",
           "video":true
       },
       "jsep":{
           "sdp":"v=0\r\no=- 8960238441194795609...",
           "type":"offer"
       },
       "userId":"stephen"
      }
     */
    @PostMapping(APIPath.OFFER)
    public IMResult<String> handleOffer(@RequestBody JSONObject jsonObject) {
        IMResult<String> result = new IMResult<>();
        String userId = jsonObject.getString("userId");
        Jsep jsep = jsonObject.getObject("jsep", Jsep.class);
        ConfigureBody configureBody = jsonObject.getObject("configureBody", ConfigureBody.class);
        String response = null;
        try {
            response = avEngineKit.sendOffer(userId, jsep, configureBody);
        } catch (Exception e) {
            e.printStackTrace();
        }
        result.setResult(response);
        return result;
    }

    /**
       {
       "configureBody":{
          "substream":0,
          "temporal":1
       },
       "targetUserId":"targetUserId",
       "userId":"stephen"
      }
     */
    @PostMapping(APIPath.CONFIGURE)
    public IMResult<String> handleConfigure(@RequestBody JSONObject jsonObject) {
        IMResult<String> result = new IMResult<>();
        String userId = jsonObject.getString("userId");
        String targetUserId = jsonObject.getString("targetUserId");
        ConfigureBody configureBody = jsonObject.getObject("configureBody", ConfigureBody.class);
        String response = null;
        try {
            response = avEngineKit.configure(userId, targetUserId ,configureBody);
        } catch (Exception e) {
            e.printStackTrace();
        }
        result.setResult(response);
        return result;
    }

    /**
      {
       candidate":"{
           "candidate":"candidate:547260449 1 udp 2122260223 10.0.2.15 38953 typ host generation 0 ufrag kT2l network-id 3 network-cost 10",
           "pin":"123456",
           "sdpMLineIndex":0,
           "sdpMid":"audio"
       }",
       "userId":"stephen",
       "targetUserId":"targetUserId"
     }
     */
    @PostMapping(APIPath.TRICKLE)
    public IMResult<String> handleCandidate(@RequestBody JSONObject jsonObject) {
        IMResult<String> result = new IMResult<>();
        String userId = jsonObject.getString("userId");
        String candidate = jsonObject.getString("candidate");
        String targetUserId = jsonObject.getString("targetUserId");
        String response = null;
        try {
            response = avEngineKit.sendCandidate(userId,targetUserId, candidate);
        } catch (Exception e) {
            e.printStackTrace();
        }
        result.setResult(response);
        return result;
    }

    /**
      {
          "userId":"stephen"
      }
     */
    @PostMapping(APIPath.KEEP_A_LIVE)
    public IMResult<String> handleKeepALive(@RequestBody JSONObject jsonObject) {
        IMResult<String> result = new IMResult<>();
        String userId = jsonObject.getString("userId");
        String response = null;
        try {
            response = avEngineKit.keepalive(userId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        result.setResult(response);
        return result;
    }

    /**
      {
          "joinBody":{
              "feed":"1604026740488360",
              "pin":"123456"
          },
          "userId":"stephen",
          "targetUserId":"targetUserId"
      }
     */
    @PostMapping(APIPath.JOIN_SUB)
    public IMResult<String> handleJoinAndSub(@RequestBody JSONObject jsonObject) {
        IMResult<String> result = new IMResult<>();
        String userId = jsonObject.getString("userId");
        String targetUserId = jsonObject.getString("targetUserId");
        JoinBody joinBody = jsonObject.getObject("joinBody", JoinBody.class);
        String response = null;
        try {
            response = avEngineKit.handleJoinAndSub(userId, targetUserId, joinBody);
        } catch (Exception e) {
            e.printStackTrace();
        }
        result.setResult(response);
        return result;
    }

    /**
      {
       "jsep":{
           "sdp":"v=0\r\no=- 3949792525869817657...",
           "type":"answer"
        },
        "userId":"stephen",
        "targetUserId":"Alice"
      }
     */
    @PostMapping(APIPath.START)
    public IMResult<String> handleStart(@RequestBody JSONObject jsonObject) {
        IMResult<String> result = new IMResult<>();
        String userId = jsonObject.getString("userId");
        String targetUserId = jsonObject.getString("targetUserId");
        Jsep jsep = jsonObject.getObject("jsep", Jsep.class);
        String response = null;
        try {
            response = avEngineKit.start(userId, targetUserId, jsep);
        } catch (Exception e) {
            e.printStackTrace();
        }
        result.setResult(response);
        return result;
    }

    /**
      {
          "userId":"stephen"
      }
     */
    @PostMapping(APIPath.LEAVE)
    public IMResult<String> handleLeave(@RequestBody JSONObject jsonObject) {
        IMResult<String> result = new IMResult<>();
        String userId = jsonObject.getString("userId");
        String response = null;
        try {
            response = avEngineKit.handleLeave(userId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        result.setResult(response);
        return result;
    }

    /**
     {
     "userId":"stephen"
     }
     */
    @PostMapping(APIPath.START_LIVE)
    public IMResult<String> handleStartLive(@RequestBody JSONObject jsonObject) {
        IMResult<String> result = new IMResult<>();
        String userId = jsonObject.getString("userId");
        String response = null;
        try {
            response = avEngineKit.startLive(userId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        result.setResult(response);
        return result;
    }

    /**
     {
     "userId":"stephen"
     }
     */
    @PostMapping(APIPath.STOP_LIVE)
    public IMResult<String> handleStopLive(@RequestBody JSONObject jsonObject) {
        IMResult<String> result = new IMResult<>();
        String userId = jsonObject.getString("userId");
        String response = null;
        try {
            response = avEngineKit.stopLive(userId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        result.setResult(response);
        return result;
    }

    /**
     {
     "userId":"stephen"
     "roomId":123456
     }
     */
    @PostMapping(APIPath.WATCH_LIVE)
    public IMResult<String> handleWatchLive(@RequestBody JSONObject jsonObject) {
        IMResult<String> result = new IMResult<>();
        String userId = jsonObject.getString("userId");
        int roomId = jsonObject.getInteger("roomId");
        String response = null;
        try {
            response = avEngineKit.watchLive(userId,roomId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        result.setResult(response);
        return result;
    }
}
